//
//  ViewController.m
//  3DTouchDemo
//
//  Created by kuaikuaizuche on 15/12/17.
//  Copyright © 2015年 JanzTam. All rights reserved.
//

#import "ViewController.h"
#import "PreviewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UIViewControllerPreviewingDelegate>
{
    UITableView * _tableView;
    NSArray * dataSources;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    dataSources = @[@"库里",@"甜瓜",@"KG",@"呆瓜"];
    
    _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return dataSources.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.textLabel.text = dataSources[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(showPeek:)];
    [cell.contentView addGestureRecognizer:longPress];
    [self registerForPreviewingWithDelegate:(id)self sourceView:cell];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PreviewController *previewController = [[PreviewController alloc] init];
    [self.navigationController pushViewController:previewController animated:YES];
}

- (void)showPeek:(UILongPressGestureRecognizer *)sender {
    sender.enabled = NO;
    PreviewController * vc = [[PreviewController alloc] init];
    
    UIViewController * topVC = [self grabTopViewController];
    [topVC showViewController:vc sender:self];
}

- (UIViewController *)grabTopViewController {
    UIViewController *top = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (top.presentedViewController) {
        top = top.presentedViewController;
    }
    return top;
}

# pragma mark - 3D Touch Delegate

- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {

    PreviewController *previewController = [[PreviewController alloc] init];
    
    return previewController;
}

- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
    
    // deep press: bring up the commit view controller (pop)
    PreviewController *commitController = [[PreviewController alloc] init];
    
    [self showViewController:commitController sender:self];
    
    // alternatively, use the view controller that's being provided here (viewControllerToCommit)
}

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
    
    // called when the interface environment changes
    // one of those occasions would be if the user enables/disables 3D Touch
    // so we'll simply check again at this point
    
}

- (NSArray<id<UIPreviewActionItem>>*)previewActionItems {
    UIPreviewAction * action0 = [UIPreviewAction actionWithTitle:@"编辑" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        
    }];
    
    UIPreviewAction * action1 = [UIPreviewAction actionWithTitle:@"取消关注" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        
    }];
    UIPreviewAction * action2 = [UIPreviewAction actionWithTitle:@"屏蔽他" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        
    }];
    
    return @[action0,action1,action2];
}

@end
